﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Linq;

using ProjectBase.Core;
using $safeprojectname$.Infrastructure.Exceptions;
using $safeprojectname$.Infrastructure.Factories;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace $safeprojectname$.Publishing.Handlers
{
    [TestClass]
    public class EventLogErroCodeTest : TestBase
    {
        [TestMethod]
        public void Known_BaseException_Passed()
        {
            LoggingTargets.RestoreLogs();

            var provider = ProviderFactory.ProviderExceptionOnly();
            var exception = new BaseException("BaseException", new BaseException());
            provider.Publish(this, Severity.Debug, exception, "Exception");

            // win event log
            var entry = LoggingTargets.TestEventLogEntries.First();
            Assert.IsTrue(entry.EntryType.Equals(EventLogEntryType.SuccessAudit));
            Assert.IsTrue(entry.InstanceId.Equals(5501));
            Assert.IsTrue(entry.Message.Contains("BaseException"));
        }

        [TestMethod]
        public void Known_ChildException_Passed()
        {
            LoggingTargets.RestoreLogs();

            var provider = ProviderFactory.ProviderExceptionOnly();
            var exception = new ChildException("ChildException");
            provider.Publish(this, "Exception", exception);

            // win event log
            var entry = LoggingTargets.TestEventLogEntries.First();
            Assert.IsTrue(entry.EntryType.Equals(EventLogEntryType.FailureAudit));
            Assert.IsTrue(entry.InstanceId.Equals(5505));
            Assert.IsTrue(entry.Message.Contains("ChildException"));
        }

        [TestMethod]
        public void Known_ChildInformationException_Passed()
        {
            LoggingTargets.RestoreLogs();

            var provider = ProviderFactory.ProviderExceptionOnly();
            var exception = new ChildInformationException("ChildInformationException");
            provider.Publish(this, "Exception", exception);

            // win event log
            var entry = LoggingTargets.TestEventLogEntries.First();
            Assert.IsTrue(entry.EntryType.Equals(EventLogEntryType.Information));
            Assert.IsTrue(entry.InstanceId.Equals(5508));
            Assert.IsTrue(entry.Message.Contains("ChildInformationException"));
        }

        [TestMethod]
        public void Null_Passed()
        {
            LoggingTargets.RestoreLogs();

            var provider = ProviderFactory.DefaultProvider();
            ChildInformationException exception = null;
            provider.Publish(this, "Exception", exception);

            // win event log
            var entry = LoggingTargets.TestEventLogEntries.FirstOrDefault();
            Assert.IsTrue(entry.IsNull());
        }

        [TestMethod]
        public void UnKnown_ChildException_Passed()
        {
            LoggingTargets.RestoreLogs();

            var provider = ProviderFactory.ProviderExceptionOnly();
            var exception = new NotMappedChildException("NotMappedChildException");
            provider.Publish(this, "Exception", exception);

            // win event log
            var entry = LoggingTargets.TestEventLogEntries.First();
            Assert.IsTrue(entry.EntryType.Equals(EventLogEntryType.SuccessAudit));
            Assert.IsTrue(entry.InstanceId.Equals(5501));
            Assert.IsTrue(entry.Message.Contains("NotMappedChildException"));
        }

        [TestMethod]
        public void UnKnown_NotMappedException_Passed()
        {
            LoggingTargets.RestoreLogs();

            var provider = ProviderFactory.ProviderExceptionOnly();
            var exception = new NotMappedException("NotMappedException");
            provider.Publish(this, "Exception", exception);

            // win event log
            var entry = LoggingTargets.TestEventLogEntries.First();
            Assert.IsTrue(entry.EntryType.Equals(EventLogEntryType.Error));
            Assert.IsTrue(entry.InstanceId.Equals(9999));
            Assert.IsTrue(entry.Message.Contains("NotMappedException"));
        }

        #region Init

        // You can use the following additional attributes as you write your tests:

        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }

        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }

        [TestInitialize]
        public override void MyTestInitialize()
        {
            base.MyTestInitialize(); // Use TestInitialize to run code before running each test
        }

        [TestCleanup]
        public override void MyTestCleanup()
        {
            base.MyTestCleanup(); // Use TestCleanup to run code after each test has run
        }
        #endregion Init
    }
}
